﻿// <copyright file="uUnit.cs" company="Sky Sanders">
// This source is a Public Domain Dedication. 
// http://salientqc.codeplex.com
// Attribution is appreciated.
// </copyright> 

// added parameterized run

using System;

namespace Salient.QualityControl
{

    /// <summary>
    /// uUnit
    /// A micro test runner.
    /// 
    /// For those times when just a little is just enough.
    /// 
    /// Simply wraps a void method in a try/catch and writes pass/fail to console.
    /// Find nested a simple assertion class.
    /// 
    /// If you need more, should probably break out the references to nunit.framework.
    ///
    /// </summary>
    public static class uUnit
    {
        #region Delegates

        public delegate void TestDelegate();
        public delegate void ParameterizedTestDelegate(params object[] args);

        #endregion
        public static void Pause()
        {
            Pause(string.Empty);
        }
        public static void Pause(string message)
        {
            Console.WriteLine(message);
            Console.WriteLine("Press enter to continue...");
            Console.ReadLine();

        }
        public static void Run(ParameterizedTestDelegate test, params object[] args)
        {
            string name = test.Method.Name;
            try
            {
                test.Invoke(args);
                Console.WriteLine(string.Format("{0} : PASSED", name));
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("{0} : FAILED : {1}", name, ex.Message));
            }
        }

        public static void Run(TestDelegate test)
        {
            string name = test.Method.Name;
            try
            {
                test.Invoke();
                Console.WriteLine(string.Format("{0} : PASSED", name));
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("{0} : FAILED : {1}", name, ex.Message));
            }
        }

        #region Nested type: Assert

        /// <summary>
        /// simple assertions. 
        /// If you need more should probably break out the ol' NUnit.
        /// </summary>
        public static class Assert
        {
            public static void AreEqual(object expected, object actual)
            {
                AreEqual(expected, actual, "");
            }

            public static void AreEqual(object expected, object actual, string message)
            {
                if (!expected.Equals(actual))
                {
                    throw new Exception(string.Format("{2}: expected {0} but got {1}", expected, actual, message));
                }
            }

            public static void IsNull(object target)
            {
                IsNull(target, "");
            }

            public static void IsNull(object target, string message)
            {
                if (target != null)
                {
                    throw new Exception(string.Format("{0}: Expected null but got {1}", message, target));
                }
            }

            public static void IsNotNull(object target)
            {
                IsNotNull(target, "");
            }

            public static void IsNotNull(object target, string message)
            {
                if (target == null)
                {
                    throw new Exception(string.Format("{0}: got null", message));
                }
            }

            public static void IsFalse(bool expression)
            {
                IsFalse(expression, "");
            }

            public static void IsFalse(bool expression, string message)
            {
                AreEqual(false, expression, message);
            }

            public static void IsTrue(bool expression)
            {
                IsTrue(expression, "");
            }

            public static void IsTrue(bool expression, string message)
            {
                AreEqual(true, expression, message);
            }
        }

        #endregion
    }
}